package com.dob.service.impl;


import com.dob.dao.UserDao;
import com.dob.page.Page;
import com.dob.page.PageHelper;
import com.dob.reponsitory.User;
import com.dob.response.ex.Ex;
import com.dob.service.IUserService;
import com.dob.utils.MD5Util;
import com.ds.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
@DS("master")
public class UserService implements IUserService {

    @Resource
    private UserDao userDao;

    @Override
    public User validateUserName(String userName) {
        return userDao.findByUserName(userName, 1);
    }

    @Override
    public void modifyPwd(Integer id, String pwd) {
        pwd = MD5Util.getMD5String(pwd);
        userDao.resetPassword(id, pwd);
    }


    @Override
    public User register(User user) {
        /***
         * 注册用户基本信息
         * 1.校验
         * 2.加密
         * 3.保存
         */
        String userName = user.getUserName();
        String passWord = user.getPwd();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord)) {
            throw Ex.build("用户名密码错误");
        }

        User u = userDao.findByUserName(userName, 1);
        if (u != null) {
            throw Ex.build("用户已存在");
        }

        user.setPwd(MD5Util.getMD5String(passWord));
        user.setAuth(1);
        user.setIsDel(1);
        Date curDate = new Date();
        user.setCreateTime(curDate);
        user.setUpdateTime(curDate);
        userDao.save(user);
        return user;
    }


    @Override
    public User login(User user) {
        /**
         * 登陆操作
         *  1:参数校验
         *  2:那用户名查询是否存在
         *  3:密码校验
         *  4:返回用户信息
         */
        String userName = user.getUserName();
        String pwd = user.getPwd();

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd)) {
            throw Ex.build("用户名或密码错误");
        }

        User u = userDao.findByUserName(userName, 1);
        if (u == null) {
            throw Ex.build("账号不存在");
        }

        pwd = MD5Util.getMD5String(pwd);

        if (!pwd.equals(u.getPwd())) {
            throw Ex.build("用户名或密码错误");
        }
        return u;
    }


    @Override
    public User resetPwd(Integer id, String newPassword) {
        if (id == null || StringUtils.isBlank(newPassword)) {
            throw Ex.build("参数不能为空");
        }

        User u = userDao.findById(id);
        if (u == null) {
            throw Ex.build("该用户不存在");
        }

        newPassword = MD5Util.getMD5String(newPassword);
        userDao.resetPassword(id, newPassword);
        return u;
    }


    @Override
    public Integer save(User user) {
        user.setIsDel(1);
        userDao.save(user);
        Integer id = user.getId();
        return id;
    }


    @Override
    public void del(Integer userId) {
        userDao.delById(userId);
    }

    @Override
    public void modify(User user) {
        if (user.getId() == null) {
            throw Ex.build("参数错误");
        }
        if (StringUtils.isNotBlank(user.getUserName())) {
            User db = userDao.findByUserName(user.getUserName(), 1);
            if (db != null) {
                if (db.getId().intValue() != user.getId()) {
                    throw Ex.build("该用户名已存在");
                }
            }
        }
        userDao.update(user);
    }

    @Override
    public void modifyPwd(Integer id, String oldPwd, String newPwd) {
        if (id == null || StringUtils.isBlank(oldPwd) || StringUtils.isBlank(newPwd)) {
            throw Ex.build("参数不能为空");
        }
        User user = userDao.findById(id);
        if (user == null) {
            throw Ex.build("账号不能为空");
        }
        String pwd = MD5Util.getMD5String(oldPwd);
        if (!pwd.equalsIgnoreCase(user.getPwd())) {
            throw Ex.build("密码错误");
        }
        newPwd = MD5Util.getMD5String(newPwd);
        userDao.resetPassword(id, newPwd);
    }

    @Override
    public Page<User> page(PageHelper helper, User user) {
        user.setIsDel(1);
        Integer count = userDao.countByPage(helper.getPage(), user);
        Page<User> page = new Page<>(helper, count);
        if (count == 0) {
            page.setContent(null);
        } else {
            List<User> list = userDao.findByPage(helper.getPage(), user);
            page.setContent(list);
        }
        return page;
    }

    @Override
    public User getById(Integer id) {
        return userDao.findById(id);
    }
}